<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 * 用户排行榜-月
 */
class UserRankMon extends Model
{
    protected $table = 'user_rank_mon';

    protected $fillable = [
        'user_id', 'rank_type', 'mon', 'rich_value', 'charm_value', 'flag', 'rank'
    ];

    public function userItem()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function userProfile()
    {
        return $this->belongsTo(UserProfile::class, 'user_id', 'user_id');
    }

    /**
     * 获取数据
     */
    static function getList($rankType = 1, $dt = '')
    {
        $where = [
            'mon' => $dt ?: date('Y-m'),
            'rank_type' => $rankType,
            'flag' => 1
        ];
        return self::where($where)->orderBy('rank', 'asc')->limit(20)->get()->toArray();
    }

    /**
     * 魅力榜记录
     */
    static function addCharmRecord($userId = 0, $rank = 1, $charmValue = 0, $mon = null)
    {
        $model = new self();
        $model->mon = $mon ?: date('Y-m');
        $model->user_id = $userId;
        $model->rank_type = 1;
        $model->rank = $rank;
        $model->charm_value = $charmValue;
        $model->flag = 1;
        return $model->saveOrFail() ? 1 : 0;
    }

    /**
     * 豪气榜记录
     */
    static function addRichRecord($userId = 0, $rank = 1, $richValue = 0, $mon = null)
    {
        $model = new self();
        $model->mon = $mon ?: date('Y-m');
        $model->user_id = $userId;
        $model->rank_type = 2;
        $model->rank = $rank;
        $model->rich_value = $richValue;
        $model->flag = 1;
        return $model->saveOrFail() ? 1 : 0;
    }
}
